package com.zzh.lib.views.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.zzh.lib.views.adapter.holder.HSuperViewHolder;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

/**
 * Created by Administrator.
 *
 * @date: 2018/5/19/019
 * @email: zzh_hz@126.com
 * @QQ: 1299234582
 * @author: zzh
 * @since 1.0
 * 基类适配器。适配多布局复杂的列表。
 */
public abstract class HBaseListAdapter<T> extends RecyclerView.Adapter<HSuperViewHolder> {
    protected Context mContext;
    protected LayoutInflater mInflater;

    protected List<T> mDataList = new ArrayList<>();

    public HBaseListAdapter(Context context) {
        mContext = context;
        mInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    }

    @NonNull
    @Override
    public HSuperViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View itemView = mInflater.inflate(getLayoutId(viewType), parent, false);
        return new HSuperViewHolder(itemView);
    }

    @Override
    public void onBindViewHolder(HSuperViewHolder holder, int position) {
        onBindItemHolder(holder, position);
    }

    public T getItem(int position) {
        return getDataList().get(position);
    }

    //局部刷新关键：带payload的这个onBindViewHolder方法必须实现
    @Override
    public void onBindViewHolder(@NonNull HSuperViewHolder holder, int position, List<Object> payloads) {
        if (payloads.isEmpty()) {
            onBindViewHolder(holder, position);
        } else {
            onBindItemHolder(holder, position, payloads);
        }

    }

    public abstract int getLayoutId(int viewType);

    public abstract void onBindItemHolder(HSuperViewHolder holder, int position);

    public void onBindItemHolder(HSuperViewHolder holder, int position, List<Object> payloads) {

    }

    @Override
    public int getItemCount() {
        return mDataList.size();
    }

    public List<T> getDataList() {
        return mDataList;
    }

    public void setDataList(List<T> list) {
        setNewInstance(list);
    }

    public void setNewInstance(List<T> data) {
        if (data != null) {
            this.mDataList = data;
        } else {
            mDataList.clear();
        }
        notifyDataSetChanged();
    }

    public void addAll(Collection<T> list) {
        if (list == null || list.isEmpty()) {
            return;
        }
        int lastIndex = this.mDataList.size();
        if (this.mDataList.addAll(list)) {
            notifyItemRangeInserted(lastIndex, list.size());
        }
    }

    public void remove(int position) {
        this.mDataList.remove(position);
        notifyItemRemoved(position);

        if (position != (getDataList().size())) { // 如果移除的是最后一个，忽略
            notifyItemRangeChanged(position, this.mDataList.size() - position);
        }
    }

    public void clear() {
        setNewInstance(null);
    }

}
